package org.learn.java.mq.localTransactionExecuter;

import java.util.Collections;
import java.util.Map;

import org.learn.java.mq.producer.TransactionProducerTest;
import org.learn.java.mq.service.BusinessService;
import org.learn.java.mq.service.BusinessServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.transaction.LocalTransactionExecuter;
import com.aliyun.openservices.ons.api.transaction.TransactionStatus;

public class MyLocalTransactionExecuter implements LocalTransactionExecuter {

	public static Logger logger = LoggerFactory.getLogger(MyLocalTransactionExecuter.class);
	private BusinessService businessService = new BusinessServiceImpl();
	private Map<String, Object> paramMap = Collections.EMPTY_MAP;
	boolean isCommit = true;
	TransactionStatus transactionStatus = TransactionStatus.Unknow;
	public TransactionStatus execute(Message msg, Object arg) {
		
		logger.debug("msgId:"+msg.getMsgID());
		/**
		try {
			new String(MessageDigest.getInstance("MD5").digest(msg.getBody()),"UTF-8");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		*/
		//业务执行
		isCommit = businessService.excuteBusinessService(paramMap);
		
		if(isCommit){
			transactionStatus = TransactionStatus.CommitTransaction;
		}else{
			transactionStatus = TransactionStatus.RollbackTransaction;
		}
		
		logger.debug("name:"+transactionStatus.name());
		return transactionStatus;
	}

	public void setParam(Map<String, Object> paramMap){
		this.paramMap = paramMap;
	}
}
